package com.yiyang.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.pojo.Building;
import com.yiyang.pojo.Result;
import com.yiyang.pojo.Room;
import com.yiyang.service.RoomService;
import com.yiyang.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author ROG
* @description 针对表【room】的数据库操作Service实现
* @createDate 2024-07-09 08:25:03
*/
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{

    @Resource
    private RoomMapper roomMapper;

    @Override
    public Result<List<Room>> getRoomList(Integer currentPage, Integer pageSize, String name, String  floor) {
        Page<Room> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .eq(StringUtils.isNotBlank(floor),"floor",floor);

        this.roomMapper.selectPage(page,wrapper);
        List<Room> list = page.getRecords();
        if (list.isEmpty()) {
            return Result.fail();
        }else{
            return Result.success(list,page.getTotal());
        }
    }

    @Override
    public Result<Void> addRoom(Room room) {
        int count = this.roomMapper.insert(room);
        if (count == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<Void> updateRoom(Room room) {
        int update = this.roomMapper.updateById(room);
        if (update == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @Override
    public Result<Void> deleteRoom(Integer id) {
        int delete = this.roomMapper.deleteById(id);
        if (delete == 1){
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}




